package com.welyo.view.quizz {
	import com.welyo.view.quizz.event.QuizzEvent;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class PaperDrop extends Sprite {
		
		private var timer		:Timer;
		private var arrItem		:Array;
		
		public function PaperDrop() {
			timer = new Timer(5000);
			timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			
			arrItem = [];
			for (var i:int = 0; i < this.numChildren; i++) {
				arrItem.push(this.getChildAt(i));
			}
			
			this.alpha = 0;
		}
		
		public function show():void {
			var i:int;
			for (i = 0; i < arrItem.length; i++) {
				arrItem[i].x = Math.random() * 450;
				arrItem[i].y = Math.random() * 80;
			}
			
			this.alpha = 1;
			timer.start();
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		
		public function hide():void {
			TweenLite.to(this, 0.75, {alpha:0, onComplete:onCompleteHideHandler});
		}
		
		private function onEnterFrameHandler(evt:Event):void {
			var i:int;
			var direction:int = 1;
			for (i = 0; i < arrItem.length; i++) {
				if (i % 2 == 0) {
					arrItem[i].y += 2;
				} else {
					arrItem[i].y += 1;
				}
				
				arrItem[i].x += direction;
				direction = direction * -1;
			}
		}
		
		private function onCompleteHideHandler():void {
			removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			
			dispatchEvent(new QuizzEvent(QuizzEvent.DROP_COMPLETE));
		}
		
		private function onTimerHandler(evt:TimerEvent):void {
			timer.stop();
			hide();
		}
	}
}